package com.gewu.bluetoothflow.tools

import android.text.Layout
import android.util.Log
import android.widget.GridLayout
import com.gewu.bluetoothflow.bean.FlowCount
import jxl.Workbook
import jxl.format.Alignment
import jxl.format.VerticalAlignment
import jxl.write.Label
import jxl.write.WritableCellFormat
import jxl.write.WritableFont
import jxl.write.WriteException
//import org.apache.poi.ss.usermodel.Cell
//import org.apache.poi.xssf.usermodel.XSSFWorkbook
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.*

object CreateExcelFile {
    fun startCreateFile(filePath: String, dataList: MutableList<FlowCount>): Boolean{
        if (filePath.isNullOrEmpty()) return false
        try {
            val titleList =
                arrayListOf("进人数", "出人数", "记录时间")
            val workbook = Workbook.createWorkbook(File(filePath))
            // 设置sheet名称
            val sheet = workbook.createSheet("客流数据", 0)

            // 合并单元格
            sheet.mergeCells(0, 0, titleList.size - 1, 0)
            sheet.mergeCells(0, 1, titleList.size - 1, 1)

            // 调整列宽
            sheet.setRowView(0, 800)
            sheet.setRowView(1, 600)
            sheet.setRowView(2, 500)

            //创建字体，参数1：字体样式，参数2：字号，参数3：粗体
            val font = WritableFont(WritableFont.createFont("宋体"), 18, WritableFont.BOLD);
            val wcTop = WritableCellFormat(font)
            wcTop.run {
                // 垂直居中
                alignment = Alignment.CENTRE
                verticalAlignment = VerticalAlignment.CENTRE
                wrap = true
            }
            // 顶部标题
            sheet.addCell(Label(0, 0, "客流记录导出表", wcTop))

            // 次级标题
            val wcTop2 = WritableCellFormat(
                WritableFont(
                    WritableFont.createFont("宋体"),
                    12
                )
            )
            wcTop2.run {
                alignment = Alignment.CENTRE
                verticalAlignment = VerticalAlignment.CENTRE
                wrap = true
            }
            //X月X日-Y月Y日
         //   sheet.addCell(Label(0, 1, "(0801 - 0815)", wcTop2))


            // 内容标题栏
            val startRow = 3

            titleList.forEachIndexed { index, title ->
                run {

                    val wc = WritableCellFormat(
                        WritableFont(
                            WritableFont.createFont("宋体"),
                            12,
                            WritableFont.BOLD
                        )
                    )
                    wc.run {
                        verticalAlignment = VerticalAlignment.CENTRE
                        wrap = true
                    }
                    sheet.addCell(Label(index, startRow - 1, title, wc))
                }
            }

            // 内容
            dataList.forEachIndexed { index, excelBean ->
                run {
                    val wc = WritableCellFormat()
                    wc.wrap = true

                    var time = excelBean.time
                    if (excelBean.time.length >= 8) {
                        val date = excelBean.time.substring(0, 2) + "-" + excelBean.time.substring(
                            2,
                            4
                        ) + "-" + excelBean.time.substring(4, 6) + " " + excelBean.time.substring(6, 8)
                        time= date //dateFormat.format(data.time)   //"24-08-12 14"
                    }else{
                        time = excelBean.time  //"24-08-12 14"
                    }

                    val valueList = arrayListOf(
                        excelBean.`in`.toString(),
                        excelBean.out.toString(),
                        time,
                    )
                    valueList.forEachIndexed { indexC, value ->
                        sheet.addCell(Label(indexC, index + startRow, value, wc))
                    }

                }
            }
            workbook.write()
            workbook.close()
            Log.e("CreateExcelFile", "导出成功")
        } catch (e: IOException) {
            e.printStackTrace()
        } catch (e: WriteException) {
            e.printStackTrace()
        }
        return true
    }


 //   fun startCreate(path: String, data: MutableList<FlowCount>){
//        val  workbook = XSSFWorkbook()
//        val sheet= workbook.createSheet("客流")
//
//        var arrayList = mutableListOf<Map<Int,String>> ()
//        var m = mutableMapOf<Int,String>()
//        m.put(0,"进入数")
//        m.put(1,"出入数")
//        m.put(2,"记录时间")
//        arrayList.add(m)
//
//        data.forEach {
//            var temp = mutableMapOf<Int,String>()
//            temp.put(0,it.`in`.toString())
//            temp.put(1,it.out.toString())
//            temp.put(2,it.time)
//            arrayList.add(temp)
//        }
//
       // var cell :Cell
//        val size = arrayList.size
//        for ((index, map) in arrayList.withIndex()) {
//            val row = sheet.createRow(index)
//           var i = 0
//            for (i in 0 until  size){
//              cell = row.createCell(i)
//              cell.setCellValue(map.get(i))
//            }
//        }
//
//        try {
//            val name = timeFormat.format(System.currentTimeMillis())
//            val files = File(path)
//            if (!files.exists()){
//                files.mkdir()
//            }
//            val file = File(path,name+"_客流.xlsx")
//            val exit = file.createNewFile()
//            if (exit) {
//                val outputStream = FileOutputStream(file)
//                workbook.write(outputStream)
//                outputStream.close()
//            }else{
//                Log.d("CreateExcelFile","create file fall")
//            }
//        }catch (e:IOException){
//            e.printStackTrace()
//        }

 //  }
}